package com.huicoo.forestmanager.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.network.bean.patrol.VerifyEventCommentBean.EventCommentEntity;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.glide.ExploreImageLoader;

import java.util.List;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/11		    Administrator
 * 核实问题对话
 */
public class VerifyEventCommentAdapter extends BaseQuickAdapter<EventCommentEntity, BaseViewHolder> {
    private OnCommentListener listener;

    public VerifyEventCommentAdapter(@Nullable List<EventCommentEntity> data) {
        super(R.layout.adapter_verify_event_comment, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, EventCommentEntity item) {
        String userName = item.UserName;
        if (TextUtils.isEmpty(userName)) {
            userName = "  ";
        }
        userName += "：";
        String comment = userName + (TextUtils.isEmpty(item.Description) ? "" : item.Description);

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(comment);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#003B92"));
        stringBuilder.setSpan(foregroundColorSpan, 0, userName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_comment, stringBuilder);

        TextView voice = helper.itemView.findViewById(R.id.voice);
        if (!TextUtils.isEmpty(item.VoicePath)) {
            voice.setVisibility(View.VISIBLE);
            String voicePath;
            if (item.VoicePath.contains("\\|")) {
                voicePath = item.VoicePath.split("\\|")[0];
            } else {
                voicePath = item.VoicePath;
            }
            voice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        LogUtils.v("gogogo get voice path = " + voicePath);
                        listener.playVoice(item.VoicePath);
                    }
                }
            });
        } else {
            voice.setVisibility(View.GONE);
        }

        ImageView pic = helper.itemView.findViewById(R.id.pic);
        if (!TextUtils.isEmpty(item.ImagePath)) {
            pic.setVisibility(View.VISIBLE);
            String imagePath;
            if (item.ImagePath.contains("\\|")) {
                imagePath = item.ImagePath.split("\\|")[0];
            } else {
                imagePath = item.ImagePath;
            }
            ExploreImageLoader.getInstance().loadImage(mContext, imagePath, pic);
            pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.showPic(imagePath);
                    }
                }
            });
        } else {
            pic.setVisibility(View.GONE);
        }

        ImageView video = helper.itemView.findViewById(R.id.video);
        if (!TextUtils.isEmpty(item.VideoPath)) {
            video.setVisibility(View.VISIBLE);
            String videoPath;
            String thumbPath;
            if (item.VideoPath.contains("\\|")) {
                videoPath = item.VideoPath.split("\\|")[0];
            } else {
                videoPath = item.VideoPath;
            }
            if (TextUtils.isEmpty(item.ThumbnailPath)) {
                video.setImageResource(R.drawable.icon_video);
            } else {
                if (item.ThumbnailPath.contains("\\|")) {
                    thumbPath = item.ThumbnailPath.split("\\|")[0];
                } else {
                    thumbPath = item.ThumbnailPath;
                }
                ExploreImageLoader.getInstance().loadImage(mContext, thumbPath, video);
            }
            video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.playVideo(videoPath);
                    }
                }
            });
        } else {
            video.setVisibility(View.GONE);
        }
    }

    public interface OnCommentListener {
        void playVoice(String path);

        void showPic(String path);

        void playVideo(String path);
    }

    public void setOnCommentListener(OnCommentListener listener) {
        this.listener = listener;
    }
}
